package org.action.jfreechart;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Shape;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.bean.O2rankgame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.StackedBarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
import org.service.GameService;

import com.opensymphony.xwork2.ActionSupport;

public class PlayCapableScaleAction extends ActionSupport{

	private GameService gameservice;
	private int uid;
	private List<O2rankgame> list;
	private JFreeChart chart;
	//页面鼠标事件时需要的参数  
	  private String hy_filename;  

	  public JFreeChart createChart(DefaultCategoryDataset defaultcategorydataset) {
		  
		  chart = ChartFactory.createBarChart3D("实力值比重分析", "歌名", "实力",  this.getDataset(),  PlotOrientation.VERTICAL,true,false,false);
		  chart.setBackgroundPaint(Color.BLACK);
	
		// 设置标题的颜色  
		   TextTitle text = new TextTitle("玩家 "+list.get(1).getNickname()+" 歌曲所占比重分析");  
	      text.setPaint(new Color(255,255,255)); 
	      text.setFont(new Font("微软雅黑", Font.BOLD, 22));
	      chart.setTitle(text);
	
	      
	      CategoryPlot plot = (CategoryPlot) chart.getPlot();
	      
	      plot.setBackgroundPaint(Color.BLACK);
	      plot.setRangeGridlinePaint(Color.BLACK);
	      
	   // 背景色 透明度     
			plot.setBackgroundAlpha(0.5f);     
		
			ValueAxis rangeAxis = plot.getRangeAxis();
			
			
			
			//X軸下標字體大小及顏色
			  Axis axis1 = plot.getDomainAxis();
			  axis1.setLabelFont(new Font("微软雅黑",Font.BOLD,20));//字體
			  axis1.setLabelPaint(Color.WHITE);//顏色
		
			 
			//X軸的字體顏色
			Axis axis = plot.getDomainAxis();
			axis.setTickLabelFont(new Font("微软雅黑", Font.BOLD,12));//字體
			axis.setTickLabelPaint(Color.WHITE);//顏色 
			 

			//Y軸字下標字體大小及顏色
			Axis axis2 = plot.getRangeAxis();
			axis2.setLabelFont(new Font("微软雅黑", Font.BOLD, 20));//字體  
			axis2.setLabelPaint(Color.WHITE); //顏色
			  
			//Y軸字體及顏色
			Axis axis3 = plot.getRangeAxis();
			axis3.setTickLabelFont(new Font("arial", Font.BOLD, 12));//字體  
			axis3.setTickLabelPaint(Color.WHITE);//顏色
			
			//换行显示
			CategoryAxis caxis = plot.getDomainAxis();   
			caxis.setMaximumCategoryLabelLines(6); // 这边的4代表最多显示4行字 

			 BarRenderer3D renderer = new BarRenderer3D();
			 renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
			 renderer.setBaseItemLabelsVisible(true);
			 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
			 renderer.setItemLabelAnchorOffset(10D);
			
			 renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator("selectmusicinfo.html", "condition", "info"));
			 
			 plot.setRenderer(renderer);
		  return chart;
	  }
	  
	  
	  private DefaultCategoryDataset getDataset() {
		  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		  
		  for(int i = 0; i < list.size(); i++) {
			  O2rankgame o2rankgame = list.get(i);
			  dataset.addValue(o2rankgame.getCapable()/10, "MusicName", o2rankgame.getMname()+" ^"+o2rankgame.getO2ranksongs().getMid());
		  }
		  return dataset;
		  
	  }
	  
	  
	  
	  
	  
	  
	  
	  
	   public String show(){  
	    HttpServletRequest req = ServletActionContext.getRequest();  
	      HttpServletResponse res = ServletActionContext.getResponse();  
	        hy_filename = createChartImage(req, res);  
	        
	     return SUCCESS;  
	   }   
	  private String createChartImage(HttpServletRequest req, HttpServletResponse res){  
	       JFreeChart chart = createChart(getDataset());  
	       String filename = createUseMap(chart, 1160, 300, req, res);  
	       return filename;  
	  }  
	  private String createUseMap(JFreeChart chart, int width, int height, HttpServletRequest req, HttpServletResponse res){  
		    //在矩形框中显示信息  
	     Shape shape = new Rectangle(20, 10);  
	   ChartEntity entity = new ChartEntity(shape);  
		       StandardEntityCollection coll = new StandardEntityCollection();  
	       coll.add(entity);  
		       //该工具类上面没有介绍，在鼠标移动到图片时显示提示信息是用Map实现的，这些Map是用该类生成的。  
		       ChartRenderingInfo info = new ChartRenderingInfo(coll);  
		      PrintWriter pw;  
		       String filename = null;  
		      try {  
		          res.setContentType("text/html;charset=utf-8");  
		           res.setCharacterEncoding("utf-8");  
		           pw = res.getWriter();//输出MAP信息   
	       
		           //写入到输出流生成图像文件，同时把图片的具体信息放入ChartRenderingInfo的一个实例为以后生成Map提供信息   
		           //ChartUtilities.writeChartAsPNG(out, chart, width, height, info);  
		         filename = ServletUtilities.saveChartAsPNG(chart, width , height, info, req.getSession());//保存图表为文件  
		            //读取info对象，生成Map信息。这些信息写在pw的输出流中，这里的输出流就是Response.out，也就是直接输出到页面了  
		          ChartUtilities.writeImageMap(pw, filename, info, false);  
		              pw.flush();  
		 
		       } catch (IOException e) {  
		             e.printStackTrace();  
		        }  
		        return filename;  
		   }
	public GameService getGameservice() {
		return gameservice;
	}
	public void setGameservice(GameService gameservice) {
		this.gameservice = gameservice;
	}
	public String getHy_filename() {
		return hy_filename;
	}
	public void setHy_filename(String hy_filename) {
		this.hy_filename = hy_filename;
	}


	public int getUid() {
		return uid;
	}


	public void setUid(int uid) {
		this.uid = uid;
	}


	public List<O2rankgame> getList() {
		return list;
	}


	public void setList(List<O2rankgame> list) {
		this.list = list;
	}  
	
	  
	  
}
